//
//  VisitorLoginView.swift
//  波波微博
//
//  Created by wave on 15/7/29.
//  Copyright © 2015年 wave. All rights reserved.
//

import UIKit
//  代理步骤:
//MARK: - ①定义协议
protocol VisitorLoginViewDelegate:NSObjectProtocol{

    //将要登陆
    func visitorLoginViewWillLogin()
    //将要注册
    func visitorLoginViewWillRegister()
}

//MARK: - VisitorLoginView
class VisitorLoginView: UIView {

    // ② 定义代理
    weak var delegate:VisitorLoginViewDelegate?
    
    //MARK: - 设置视图信息,供别的控制器调用并且设置文字和图片
    func setupViewInfo(isHome:Bool,title:String,imageName:String){
    
        describeText.text = title
        
        homeIconView.image = UIImage(named: imageName)
        
        iconView.hidden = !isHome
        
        //判断是否是第一个页面,如果是就让iconView旋转,如果不是则添加阴影
        isHome ? startAnimation() : sendSubviewToBack(maskIconView)
    
    }
    
    //动画旋转方法
    private func startAnimation(){
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20.0
        
        //  设置了这个属性可以当界面切换或者退出界面的时候动画不会停止
        anim.removedOnCompletion = false
        
        //将让图片旋转的这些属性添加到iconview上面
        homeIconView.layer.addAnimation(anim, forKey: nil)
    }
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()

    }

    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        setUpUI()
    }
    
    
    
    //MARK: - 添加约束
    private func setUpUI(){
    
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(homeIconView)
        addSubview(describeText)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        // 1> 图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: -40))
        
        // 2> 小房子 - 用代码开发时，最好所有相关的控件最好有一个统一的参照物
        homeIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1.0, constant: 0))
        
        // 3> 消息文字
        describeText.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: describeText, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 224))
        
        // 4> 注册按钮
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: describeText, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeText, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 5> 登录按钮
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: describeText, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describeText, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 35))
        
        // 6> 遮罩视图 VFL 可视化格式语言
        // 美工提供的素材，大多是纯色的
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-(-35)-[regButton]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview": maskIconView, "regButton": registerBtn]))
        
        // 7> 设置背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
        
    }
    
    
    //MARK: - 创建控件
    //图标
    private lazy var iconView:UIImageView = {
    
        let v = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
        
        return v
        
    }()
    
    //阴影遮罩
    private lazy var maskIconView:UIImageView = {
        
        let v = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        
        return v
    }()
    
    //小房子
    private lazy var homeIconView:UIImageView = {
        
        let v = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))

        return v
    }()

    //描述文字
    private lazy var describeText:UILabel = {
        
        let label = UILabel()
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        label.font = UIFont.systemFontOfSize(14)
        
        label.sizeToFit()
        
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn:UIButton = {
        
        let register = UIButton()
        
        register.setTitle("注册", forState: UIControlState.Normal)
        
        register.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        register.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //③  给按钮添加监听方法
        register.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)
        
        return register
    }()
    
    //登陆按钮
    private lazy var loginBtn:UIButton = {
    
        let login = UIButton()
        
        login.setTitle("登陆", forState: UIControlState.Normal)
        
        login.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        login.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        //③  给按钮添加监听方法
        login.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)

        return login
    }()
    
    
    //③  给按钮添加监听方法
    func clickLogin(){
        
        //④ 给代理
        delegate?.visitorLoginViewWillLogin()
    }
    
    func clickRegister(){
        
        //④ 给代理
        delegate?.visitorLoginViewWillRegister()
    }
    
}
















